﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPMS.Utility
{
    /// <summary>
    /// 参数断言类
    /// </summary>
    /// <remarks>用于断言判断条件的True/False命题</remarks>
    /// <history>
    /// [zanezeng]   9/18/2008 5:08 PM    Created
    /// </history>
    public static class ArgumentAssert
    {
        /// <summary>
        /// 断言给定的参数值为给定的合法值中的一个
        /// </summary>
        /// <param paramName="value">被断言的参数的参数值</param>
        /// <param paramName="paramName">被断言参数的名称</param>
        /// <param paramName="legalValues">合法的参数值列表</param>
        /// <remarks>如果合法值列表为<c>null</c>引用或者为空集合，则总是认为合法。</remarks>
        /// <history>
        /// [zanezeng]               2010/1/5 13:30    创建
        /// </history>
        public static void AssertArgumentInRange( object value, string paramName, params object[] legalValues )
        {
            //判断集合是否为空引用或者为空集合
            if (null == legalValues || 0 == legalValues.Length)
            {
                //如果为空引用或者为空集合，则直接返回
                return;
            }

            //判断参数值是否不属于集合，如果不属于集合则抛出异常
            if (-1 == Array.IndexOf<object>( legalValues, value ))
            {
                //生成提示消息
                string message = string.Format( Properties.Resources.MSG_INCLUDE_REQUIRED, StringUtility.JoinObjects( legalValues, "," ) );

                //如果参数值不属于集合，则抛出异常
                throw new ArgumentException( message, paramName );
            }
        }

        /// <summary>
        /// 断言给定的参数值不是给定的合法值中的任何一个
        /// </summary>
        /// <param paramName="value">被断言的参数的参数值</param>
        /// <param paramName="paramName">被断言参数的名称</param>
        /// <param paramName="legalValues">合法的参数值列表</param>
        /// <remarks>如果合法值列表为<c>null</c>引用或者为空集合，则总是认为合法。</remarks>
        /// <history>
        /// [zanezeng]               2010/1/5 13:30    创建
        /// </history>
        public static void AssertArgumentNotInRange( object value, string paramName, params object[] legalValues )
        {
            //判断集合是否为空引用或者为空集合
            if (null == legalValues || 0 == legalValues.Length)
            {
                //如果为空引用或者为空集合，则直接返回
                return;
            }

            //判断参数值是否属于集合，如果属于集合则抛出异常
            if (-1 != Array.IndexOf<object>( legalValues, value ))
            {
                //生成提示消息
                string message = string.Format( Properties.Resources.MSG_EXCLUDE_REQUIRED, StringUtility.JoinObjects( legalValues, "," ) );

                //如果参数值不属于集合，则抛出异常
                throw new ArgumentException( message, paramName );
            }
        }

        /// <summary>
        /// 断言给定的参数值不为空引用
        /// </summary>
        /// <param paramName="value">被断言的参数的参数值</param>
        /// <param paramName="paramName">被断言参数的名称</param>
        /// <exception cref="System.ArgumentNullException"><paramref paramName="value"/>为<c>null</c>引用。</exception>
        /// <remarks>如果断言失败，将抛出异常</remarks>
        /// <history>
        /// [zanezeng]   9/18/2008 5:12 PM    Created
        /// </history>
        public static void AssertArgumentNotNull( object value, string paramName )
        {
            //判断传入的参数是否为空
            if (null == value)
            {
                //抛出异常
                throw new ArgumentNullException( paramName );
            }
        }

        /// <summary>
        /// 断言给定的字符串参数不为空引用
        /// </summary>
        /// <param paramName="value">被断言的字符串参数的参数值</param>
        /// <param paramName="paramName">被断言的字符串参数的名称</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/5 13:22    创建
        /// </history>
        public static void AssertArgumentNotNull( string value, string paramName )
        {
            //判断传入的参数是否为空
            if (null == value)
            {
                //抛出异常
                throw new ArgumentNullException( paramName );
            }
        }

        /// <summary>
        /// 断言给定的字符串参数不为空引用或者为空字符串
        /// </summary>
        /// <param paramName="value">被断言的字符串参数的参数值</param>
        /// <param paramName="paramName">被断言的字符串参数的名称</param>
        /// <exception cref="System.ArgumentNullException">value为Null或者为空</exception>
        /// <remarks>如果断言失败，将抛出异常</remarks>
        /// <history>
        /// [zanezeng]   9/18/2008 5:12 PM    Created
        /// </history>
        public static void AssertStringArgumentNotNullOrEmpty( string value, string paramName )
        {
            //判断传入的参数是否为空
            if (string.IsNullOrEmpty( value ))
            {
                //抛出异常
                throw new ArgumentNullException( paramName, Properties.Resources.MSG_STRING_NULLOREMPTY );
            }
        }

        /// <summary>
        /// 断言给定的字符串参数不为空字符串
        /// </summary>
        /// <param paramName="value">被断言的字符串参数的参数值</param>
        /// <param paramName="paramName">被断言的字符串参数的名称</param>
        /// <exception cref="System.ArgumentNullException">value为Null或者为空</exception>
        /// <remarks>如果断言失败，将抛出异常</remarks>
        /// <history>
        /// [zanezeng]   9/18/2008 5:12 PM    Created
        /// </history>
        public static void AssertStringArgumentNotEmpty( string value, string paramName )
        {
            //判断传入的参数是否为空字符串
            if (string.IsNullOrEmpty( value ))
            {
                //抛出异常
                throw new ArgumentException( paramName, Properties.Resources.MSG_STRING_EMPTY );
            }
        }

        /// <summary>
        /// 断言给定的集合参数值不为空引用或者为空集合
        /// </summary>
        /// <param paramName="value">被断言的对象</param>
        /// <param paramName="paramName">参数的名称</param>
        /// <exception cref="System.ArgumentNullException"><paramref paramName="value"/>为<c>null</c>引用或者集合的长度为0。</exception>
        /// <remarks>如果断言失败，将抛出异常</remarks>
        /// <history>
        /// [zanezeng]   9/18/2008 5:12 PM    Created
        /// </history>
        public static void AssertCollectionArgumentNotNullOrEmpty<T>( ICollection<T> value, string paramName )
        {
            //判断传入的参数是否为空
            if (CollectionUtility.IsNullOrEmpty( value ))
            {
                //抛出异常
                throw new ArgumentNullException( paramName, Properties.Resources.MSG_COLLECTION_NULLOREMPTY );
            }
        }

        /// <summary>
        /// 断言给定的参数值在允许的范围内
        /// </summary>
        /// <param paramName="value">被断言的参数的参数值</param>
        /// <param paramName="minValue">参数值允许的最小值</param>
        /// <param paramName="maxValue">参数值允许的最大值</param>
        /// <param paramName="paramName">被断言的参数的名称</param>
        /// <exception cref="System.ArgumentOutOfRangeException">需要断言的值不在最大最小值的范围中</exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-1 11:24    Created
        /// </history>
        public static void AssertArgumentRange( int value, int minValue, int maxValue, string paramName )
        {
            //断言参数
            if (value > maxValue || value < minValue)
            {
                //创建提示消息
                string message = string.Format( Properties.Resources.MSG_VALUE_OUTOFRANGE, minValue, maxValue );

                //抛出异常
                throw new ArgumentOutOfRangeException( paramName, value, message );
            }
        }

        /// <summary>
        /// 断言给定的参数值在允许的范围内
        /// </summary>
        /// <param paramName="value">被断言的参数的参数值</param>
        /// <param paramName="minValue">参数值允许的最小值</param>
        /// <param paramName="maxValue">参数值允许的最大值</param>
        /// <param paramName="paramName">被断言的参数的名称</param>
        /// <exception cref="System.ArgumentOutOfRangeException">需要断言的值不在最大最小值的范围中</exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-1 11:24    Created
        /// </history>
        public static void AssertArgumentRange( long value, long minValue, long maxValue, string paramName )
        {
            //断言参数
            if (value > maxValue || value < minValue)
            {
                //创建提示消息
                string message = string.Format( Properties.Resources.MSG_VALUE_OUTOFRANGE, minValue, maxValue );

                //抛出异常
                throw new ArgumentOutOfRangeException( paramName, value, message );
            }
        }

        /// <summary>
        /// 断言参数的判定结果为真
        /// </summary>
        /// <param paramName="value">参数的判定结果</param>
        /// <param paramName="message">提示消息</param>
        /// <param paramName="paramName">参数的名称</param>
        /// <exception cref="ArgumentException">如果<paramref paramName="value"/>为<c>false</c>，则抛出此异常。</exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/15 10:33    创建
        /// </history>
        public static void AssertTrue( bool value, string message, string paramName )
        {
            //判断判定结果是否为假
            if (!value)
            {
                //如果为假，则抛出异常
                throw new ArgumentException( message, paramName );
            }
        }
    }
}
